# Bazel build
# C/C++ documentation: https://docs.bazel.build/versions/master/be/c-cpp.html

load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
load("@//bazel:ray.bzl", "flatbuffer_py_library")
load("@//bazel:cython_library.bzl", "pyx_library")

COPTS = ["-DRAY_USE_GLOG"]

cc_binary(
    name = "raylet",
    srcs = ["src/ray/raylet/main.cc"],
    copts = COPTS,
    visibility = ["//java:__subpackages__"],
    deps = [
        ":ray_util",
        ":raylet_lib",
    ],
)

cc_binary(
    name = "raylet_monitor",
    srcs = [
        "src/ray/raylet/monitor.cc",
        "src/ray/raylet/monitor.h",
        "src/ray/raylet/monitor_main.cc",
    ],
    copts = COPTS,
    deps = [
        ":gcs",
        ":ray_util",
    ],
)

cc_library(
    name = "stats_lib",
    srcs = glob(
        [
            "src/ray/stats/*.cc",
        ],
        exclude = [
            "src/ray/stats/*_test.cc",
        ],
    ),
    hdrs = glob(
        [
            "src/ray/stats/*.h",
        ],
    ),
    copts = COPTS,
    includes = [
        "src",
    ],
    linkopts = ["-pthread"],
    deps = [
        ":ray_util",
        "@com_github_jupp0r_prometheus_cpp//pull",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/strings",
        "@com_google_googletest//:gtest",
        "@io_opencensus_cpp//opencensus/exporters/stats/prometheus:prometheus_exporter",
        "@io_opencensus_cpp//opencensus/exporters/stats/stdout:stdout_exporter",
        "@io_opencensus_cpp//opencensus/stats",
        "@io_opencensus_cpp//opencensus/tags",
    ],
)

cc_library(
    name = "raylet_lib",
    srcs = glob(
        [
            "src/ray/raylet/*.cc",
        ],
        exclude = [
            "src/ray/raylet/mock_gcs_client.cc",
            "src/ray/raylet/monitor_main.cc",
            "src/ray/raylet/*_test.cc",
        ],
    ),
    hdrs = glob([
        "src/ray/raylet/*.h",
    ]),
    copts = COPTS,
    linkopts = ["-pthread"],
    deps = [
        ":gcs",
        ":gcs_fbs",
        ":node_manager_fbs",
        ":object_manager",
        ":ray_common",
        ":ray_util",
        ":stats_lib",
        "@boost//:asio",
        "@com_github_jupp0r_prometheus_cpp//pull",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/strings",
        "@com_google_googletest//:gtest",
        "@io_opencensus_cpp//opencensus/exporters/stats/prometheus:prometheus_exporter",
        "@io_opencensus_cpp//opencensus/stats",
        "@io_opencensus_cpp//opencensus/tags",
        "@plasma//:plasma_client",
    ],
)

cc_test(
    name = "lineage_cache_test",
    srcs = ["src/ray/raylet/lineage_cache_test.cc"],
    copts = COPTS,
    deps = [
        ":node_manager_fbs",
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "reconstruction_policy_test",
    srcs = ["src/ray/raylet/reconstruction_policy_test.cc"],
    copts = COPTS,
    deps = [
        ":node_manager_fbs",
        ":object_manager",
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "worker_pool_test",
    srcs = ["src/ray/raylet/worker_pool_test.cc"],
    copts = COPTS,
    deps = [
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "logging_test",
    srcs = ["src/ray/util/logging_test.cc"],
    args = ["--gtest_filter=PrintLogTest*"],
    copts = COPTS,
    deps = [
        ":ray_util",
        "@boost//:asio",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "task_dependency_manager_test",
    srcs = ["src/ray/raylet/task_dependency_manager_test.cc"],
    copts = COPTS,
    deps = [
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "task_test",
    srcs = ["src/ray/raylet/task_test.cc"],
    copts = COPTS,
    deps = [
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "client_connection_test",
    srcs = ["src/ray/raylet/client_connection_test.cc"],
    copts = COPTS,
    deps = [
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "signal_test",
    srcs = ["src/ray/util/signal_test.cc"],
    copts = COPTS,
    deps = [
        ":raylet_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "stats_test",
    srcs = ["src/ray/stats/stats_test.cc"],
    deps = [
        ":stats_lib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "object_manager",
    srcs = glob([
        "src/ray/object_manager/*.cc",
    ]),
    hdrs = glob([
        "src/ray/object_manager/*.h",
    ]),
    copts = COPTS,
    includes = [
        "src",
    ],
    deps = [
        ":gcs",
        ":object_manager_fbs",
        ":ray_common",
        ":ray_util",
        "@boost//:asio",
        "@plasma//:plasma_client",
    ],
)

cc_binary(
    name = "object_manager_test",
    testonly = 1,
    srcs = ["src/ray/object_manager/test/object_manager_test.cc"],
    copts = COPTS,
    deps = [
        ":object_manager",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_binary(
    name = "object_manager_stress_test",
    testonly = 1,
    srcs = ["src/ray/object_manager/test/object_manager_stress_test.cc"],
    copts = COPTS,
    deps = [
        ":object_manager",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "ray_util",
    srcs = glob(
        [
            "src/ray/*.cc",
            "src/ray/util/*.cc",
        ],
        exclude = [
            "src/ray/util/logging_test.cc",
            "src/ray/util/signal_test.cc",
        ],
    ),
    hdrs = glob([
        "src/ray/*.h",
        "src/ray/util/*.h",
    ]),
    copts = COPTS,
    includes = [
        "src",
    ],
    deps = [
        ":sha256",
        "@com_github_google_glog//:glog",
        "@plasma//:plasma_client",
    ],
)

cc_library(
    name = "ray_common",
    srcs = [
        "src/ray/common/client_connection.cc",
        "src/ray/common/common_protocol.cc",
    ],
    hdrs = [
        "src/ray/common/client_connection.h",
        "src/ray/common/common_protocol.h",
    ],
    copts = COPTS,
    includes = [
        "src/ray/gcs/format",
    ],
    deps = [
        ":gcs_fbs",
        ":node_manager_fbs",
        ":ray_util",
        "@boost//:asio",
    ],
)

cc_library(
    name = "sha256",
    srcs = [
        "src/ray/thirdparty/sha256.c",
    ],
    hdrs = [
        "src/ray/thirdparty/sha256.h",
    ],
    includes = ["src/ray/thirdparty"],
)

cc_library(
    name = "hiredis",
    srcs = glob(
        [
            "src/ray/thirdparty/ae/ae.c",
            "src/ray/thirdparty/hiredis/*.c",
        ],
        exclude = [
            "src/ray/thirdparty/hiredis/test.c",
        ],
    ),
    hdrs = glob([
        "src/ray/thirdparty/ae/*.h",
        "src/ray/thirdparty/hiredis/*.h",
        "src/ray/thirdparty/hiredis/adapters/*.h",
        "src/ray/thirdparty/hiredis/dict.c",
        "src/ray/thirdparty/ae/ae_kqueue.c",
        "src/ray/thirdparty/ae/ae_epoll.c",
    ]),
    includes = [
        "src/ray/thirdparty/ae",
        "src/ray/thirdparty/hiredis",
    ],
)

cc_library(
    name = "gcs",
    srcs = glob(
        [
            "src/ray/gcs/*.cc",
        ],
        exclude = [
            "src/ray/gcs/*_test.cc",
        ],
    ),
    hdrs = glob([
        "src/ray/gcs/*.h",
        "src/ray/gcs/format/*.h",
    ]),
    copts = COPTS,
    includes = [
        "src/ray/gcs/format",
    ],
    deps = [
        ":gcs_fbs",
        ":hiredis",
        ":node_manager_fbs",
        ":ray_common",
        ":ray_util",
        ":stats_lib",
        "@boost//:asio",
    ],
)

cc_binary(
    name = "gcs_client_test",
    testonly = 1,
    srcs = ["src/ray/gcs/client_test.cc"],
    copts = COPTS,
    deps = [
        ":gcs",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_binary(
    name = "asio_test",
    testonly = 1,
    srcs = ["src/ray/gcs/asio_test.cc"],
    copts = COPTS,
    deps = [
        ":gcs",
        ":ray_util",
        "@com_google_googletest//:gtest_main",
    ],
)

FLATC_ARGS = [
    "--gen-object-api",
    "--gen-mutable",
    "--scoped-enums",
]

flatbuffer_cc_library(
    name = "gcs_fbs",
    srcs = ["src/ray/gcs/format/gcs.fbs"],
    flatc_args = FLATC_ARGS,
    out_prefix = "src/ray/gcs/format/",
)

flatbuffer_cc_library(
    name = "common_fbs",
    srcs = ["@plasma//:cpp/src/plasma/format/common.fbs"],
    flatc_args = FLATC_ARGS,
    out_prefix = "src/ray/common/",
)

flatbuffer_cc_library(
    name = "node_manager_fbs",
    srcs = ["src/ray/raylet/format/node_manager.fbs"],
    flatc_args = FLATC_ARGS,
    include_paths = ["src/ray/gcs/format"],
    includes = [":gcs_fbs_includes"],
    out_prefix = "src/ray/raylet/format/",
)

flatbuffer_cc_library(
    name = "object_manager_fbs",
    srcs = ["src/ray/object_manager/format/object_manager.fbs"],
    flatc_args = FLATC_ARGS,
    out_prefix = "src/ray/object_manager/format/",
)

pyx_library(
    name = "_raylet",
    srcs = glob([
        "python/ray/__init__.py",
        "python/ray/_raylet.pyx",
        "python/ray/includes/*.pxd",
        "python/ray/includes/*.pxi",
    ]),
    deps = ["//:raylet_lib"],
)

cc_binary(
    name = "libraylet_library_java.so",
    srcs = [
        "src/ray/raylet/lib/java/org_ray_runtime_raylet_RayletClientImpl.h",
        "src/ray/raylet/lib/java/org_ray_runtime_raylet_RayletClientImpl.cc",
        "src/ray/id.h",
        "src/ray/raylet/raylet_client.h",
        "src/ray/util/logging.h",
        "@bazel_tools//tools/jdk:jni_header",
    ] + select({
        "@bazel_tools//src/conditions:windows": ["@bazel_tools//tools/jdk:jni_md_header-windows"],
        "@bazel_tools//src/conditions:darwin": ["@bazel_tools//tools/jdk:jni_md_header-darwin"],
        "//conditions:default": ["@bazel_tools//tools/jdk:jni_md_header-linux"],
    }),
    includes = [
        "src",
        "external/bazel_tools/tools/jdk/include",
    ] + select({
        "@bazel_tools//src/conditions:windows": ["external/bazel_tools/tools/jdk/include/windows"],
        "@bazel_tools//src/conditions:darwin": ["external/bazel_tools/tools/jdk/include/darwin"],
        "//conditions:default": ["external/bazel_tools/tools/jdk/include/linux"],
    }),
    linkshared = 1,
    linkstatic = 1,
    deps = [
        "//:raylet_lib",
        "@plasma//:plasma_client",
    ],
)

genrule(
    name = "raylet-jni-darwin-compat",
    srcs = [":libraylet_library_java.so"],
    outs = ["libraylet_library_java.dylib"],
    cmd = "cp $< $@",
    output_to_bindir = 1,
)

filegroup(
    name = "raylet_library_java",
    srcs = select({
        "@bazel_tools//src/conditions:darwin": [":libraylet_library_java.dylib"],
        "//conditions:default": [":libraylet_library_java.so"],
    }),
    visibility = ["//java:__subpackages__"],
)

filegroup(
    name = "gcs_fbs_file",
    srcs = ["src/ray/gcs/format/gcs.fbs"],
    visibility = ["//java:__subpackages__"],
)

flatbuffer_py_library(
    name = "python_gcs_fbs",
    srcs = [
        ":gcs_fbs_file",
    ],
    outs = [
        "ActorCheckpointIdData.py",
        "ActorState.py",
        "ActorTableData.py",
        "Arg.py",
        "ClassTableData.py",
        "ClientTableData.py",
        "ConfigTableData.py",
        "CustomSerializerData.py",
        "DriverTableData.py",
        "ErrorTableData.py",
        "ErrorType.py",
        "FunctionTableData.py",
        "GcsTableEntry.py",
        "HeartbeatBatchTableData.py",
        "HeartbeatTableData.py",
        "Language.py",
        "ObjectTableData.py",
        "ProfileEvent.py",
        "ProfileTableData.py",
        "RayResource.py",
        "ResourcePair.py",
        "SchedulingState.py",
        "TablePrefix.py",
        "TablePubsub.py",
        "TaskInfo.py",
        "TaskLeaseData.py",
        "TaskReconstructionData.py",
        "TaskTableData.py",
        "TaskTableTestAndUpdate.py",
    ],
    out_prefix = "python/ray/core/generated/",
)

flatbuffer_py_library(
    name = "python_node_manager_fbs",
    srcs = [
        "src/ray/raylet/format/node_manager.fbs",
    ],
    outs = [
        "ray/protocol/DisconnectClient.py",
        "ray/protocol/FetchOrReconstruct.py",
        "ray/protocol/ForwardTaskRequest.py",
        "ray/protocol/FreeObjectsRequest.py",
        "ray/protocol/GetTaskReply.py",
        "ray/protocol/MessageType.py",
        "ray/protocol/NotifyUnblocked.py",
        "ray/protocol/PushErrorRequest.py",
        "ray/protocol/RegisterClientReply.py",
        "ray/protocol/RegisterClientRequest.py",
        "ray/protocol/RegisterNodeManagerRequest.py",
        "ray/protocol/ResourceIdSetInfo.py",
        "ray/protocol/SubmitTaskRequest.py",
        "ray/protocol/Task.py",
        "ray/protocol/TaskExecutionSpecification.py",
        "ray/protocol/WaitReply.py",
        "ray/protocol/WaitRequest.py",
    ],
    include_paths = [
        "src/ray/gcs/format/",
    ],
    includes = ["src/ray/gcs/format/gcs.fbs"],
    out_prefix = "python/ray/core/generated/",
)

filegroup(
    name = "python_sources",
    srcs = glob([
        "python/ray/*.py",
        "python/ray/autoscaler/*.py",
        "python/ray/autoscaler/aws/example-full.yaml",
        "python/ray/autoscaler/gcp/example-full.yaml",
        "python/ray/autoscaler/local/example-full.yaml",
        "python/ray/cloudpickle/*.py",
        "python/ray/core/__init__.py",
        "python/ray/core/generated/__init__.py",
        "python/ray/core/generated/ray/__init__.py",
        "python/ray/core/generated/ray/protocol/__init__.py",
        "python/ray/dashboard/dashboard.py",
        "python/ray/dashboard/index.html",
        "python/ray/dashboard/res/main.css",
        "python/ray/dashboard/res/main.js",
        "python/ray/experimental/*.py",
        "python/ray/internal/*.py",
        "python/ray/workers/default_worker.py",
    ]),
)

genrule(
    name = "redis",
    outs = [
        "redis-server",
        "redis-cli",
    ],
    cmd = """
        set -x &&
        curl -sL \"https://github.com/antirez/redis/archive/5.0.3.tar.gz\" | tar xz --strip-components=1 -C . &&
        make &&
        mv ./src/redis-server $(location redis-server) &&
        chmod +x $(location redis-server) &&
        mv ./src/redis-cli $(location redis-cli) &&
        chmod +x $(location redis-cli)
    """,
    visibility = ["//java:__subpackages__"],
)

cc_binary(
    name = "libray_redis_module.so",
    srcs = [
        "src/ray/gcs/redis_module/ray_redis_module.cc",
        "src/ray/gcs/redis_module/redis_string.h",
        "src/ray/gcs/redis_module/redismodule.h",
    ],
    copts = COPTS,
    linkshared = 1,
    linkstatic = 1,
    visibility = ["//java:__subpackages__"],
    deps = [
        ":ray_common",
    ],
)

genrule(
    name = "ray_pkg",
    srcs = [
        "python/ray/_raylet.so",
        "//:python_sources",
        "//:python_gcs_fbs",
        "//:python_node_manager_fbs",
        "//:redis-server",
        "//:redis-cli",
        "//:libray_redis_module.so",
        "//:raylet",
        "//:raylet_monitor",
        "@plasma//:plasma_store_server",
    ],
    outs = ["ray_pkg.out"],
    cmd = """
        set -x &&
        WORK_DIR=$$(pwd) &&
        cp -f $(location python/ray/_raylet.so) $$WORK_DIR/python/ray &&
        mkdir -p $$WORK_DIR/python/ray/core/src/ray/thirdparty/redis/src/ &&
        cp -f $(location //:redis-server) $$WORK_DIR/python/ray/core/src/ray/thirdparty/redis/src/ &&
        cp -f $(location //:redis-cli) $$WORK_DIR/python/ray/core/src/ray/thirdparty/redis/src/ &&
        mkdir -p $$WORK_DIR/python/ray/core/src/ray/gcs/redis_module/ &&
        cp -f $(locations //:libray_redis_module.so) $$WORK_DIR/python/ray/core/src/ray/gcs/redis_module/ &&
        cp -f $(location //:raylet_monitor) $$WORK_DIR/python/ray/core/src/ray/raylet/ &&
        cp -f $(location @plasma//:plasma_store_server) $$WORK_DIR/python/ray/core/src/plasma/ &&
        cp -f $(location //:raylet) $$WORK_DIR/python/ray/core/src/ray/raylet/ &&
        for f in $(locations //:python_gcs_fbs); do cp -f $$f $$WORK_DIR/python/ray/core/generated/; done &&
        mkdir -p $$WORK_DIR/python/ray/core/generated/ray/protocol/ &&
        for f in $(locations //:python_node_manager_fbs); do 
            cp -f $$f $$WORK_DIR/python/ray/core/generated/ray/protocol/; 
        done &&
        echo $$WORK_DIR > $@
    """,
    local = 1,
)
